<template>
  <div class="login_container">
    <el-row>
      <!-- xs：屏幕小于等于768时第一个盒子不占位 -->
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <!-- 此处model是表单校验的数据源 -->
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎登录</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              @click="login"
              class="login_btn"
              type="primary"
              show-password
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue';
import { reactive, ref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
//引入消息提示
import { ElNotification } from 'element-plus';
//引入当前时间的函数
import { getTime } from '@/utils/time';
//引入用户相关小仓库
import useUserStrore from '@/store/modules/user';
let useStore = useUserStrore();
//获取路由器
let $router = useRouter();
let $route = useRoute();

//获取el-form组件
let loginForms = ref();

//定义变量控制按钮加载
let loading = ref(false);

//收集账号密码数据
let loginForm = reactive({
  username: 'admin',
  password: 'atguigu123',
});

//登录
const login = async () => {
  console.log(loginForms);

  //表单校验通过才发请求，
  await loginForms.value.validate();

  //加载效果
  loading.value = true;
  try {
    await useStore.userLogin(loginForm);

    //路由路径是否有query参数，有则跳转指定路由
    let redirect = $route.query.redirect;

    $router.push({ path: (redirect as string) || '/' });

    //编程式导航跳转到首页
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `Hi, ${getTime()}`,
    });
    loading.value = false;
  } catch (error) {
    loading.value = false;
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    });
  }
};

//自定义校验规则函数
const validatorUserName = (_rule: any, value: any, callback: any) => {
  //rule：校验规则对象
  //value：表单元素文本内容
  //callback放行即通过校验
  if (/^\w{3,10}$/.test(value)) {
    callback();
  } else {
    callback(new Error('账号长度至少为3'));
  }
};
const validatorPassword = (_rule: any, value: any, callback: any) => {
  if (/^\w{6,15}$/.test(value)) {
    callback();
  } else {
    callback(new Error('密码长度至少为6'));
  }
};

//表单校验配置对象
const rules = {
  //change：文本发生变化时触发
  username: [
    {
      validator: validatorUserName,
      trigger: 'blur',
    },
  ],
  password: [
    {
      validator: validatorPassword,
      trigger: 'blur',
    },
  ],
};
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }
    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
